<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\admin\model;
use think\Model;
use think\facade\Db;

class Area extends Model{
	protected $auto = ['level'];

    //级别：树形深度
    protected function setLevelAttr(){
    	$level = 0; $parentid = input('parentid');
    	if($parentid > 0){
    		$parentRs = Db::name('area')->find($parentid);
    		$level = $parentRs['level'] + 1;
    	}
		return $level;
    }

    //新增编辑
	static function edit($id){
	    if($id>0){//编辑
			$area = Area::find($id);
			$old_domain = $area['bind_domain'];
		}else{//新增
			$area = new Area;
			$old_domain = input('bind_domain');
		}
		$area->save(input('post.'));
		
        /*域名绑定 开始*/
        $configDomainArray = config('app.domain_bind'); $bind_domain = input('bind_domain');
        if(!empty($configDomainArray)){
            $dofile = new \common\Dofile(); $appFile = app()->getConfigPath().'app.php';
    		if(input('is_domain') == 1 and !empty($bind_domain)){
    		    // 配置文件添加域名绑定
    		    if(!array_key_exists($bind_domain,$configDomainArray)){
    		        // $configDomainArray += array($bind_domain=>config('lang.default_lang'));
    		        foreach ($configDomainArray as $bindDomain => $bindLang){
        		        $old_domain_bind .= '\''.$bindDomain.'\'=>\''.$bindLang.'\',';
                    }
                    $new_domain_bind = $old_domain_bind.'\''.$bind_domain.'\'=>\''.config('lang.default_lang').'\'';
                    $old_domain_bind = '\'domain_bind\'      => ['.rtrim($old_domain_bind,',').']';
                    $new_domain_bind = '\'domain_bind\'      => ['.$new_domain_bind.']';
                    $dofile->editFile($appFile,$old_domain_bind,$new_domain_bind);
    		    }
    		}else{
    		    // 配置文件删除域名绑定
    		    if(array_key_exists($old_domain,$configDomainArray)){
    		        // unset($configDomainArray[$old_domain]);
    		        foreach ($configDomainArray as $bindDomain => $bindLang){
                        if($bindDomain <> $old_domain){
                            $new_domain_bind .= '\''.$bindDomain.'\'=>\''.$bindLang.'\',';
                        }
        		        $old_domain_bind .= '\''.$bindDomain.'\'=>\''.$bindLang.'\',';
                    }
                    $old_domain_bind = '\'domain_bind\'      => ['.rtrim($old_domain_bind,',').']';
                    $new_domain_bind = '\'domain_bind\'      => ['.rtrim($new_domain_bind,',').']';
                    $dofile->editFile($appFile,$old_domain_bind,$new_domain_bind);
    		    }
    		}
        }
        /*域名绑定 结束*/
		
		return $area->id;
	}

	/**列表
    * lang 语言
    * listNum 每页显示数
    * keyword 关键词
    * order 排序
    */
	static function index($lang, $listNum = 15, $keyword, $order, $from = ''){
		$keyword = trim($keyword);
		$pageParam = array('lang'=>$lang);
        $pageParam += array('keyword'=>$keyword);

        $where[] = ['lang','=',$lang];
		if(empty($keyword)){
			$where[] = ['parentid','=',0];
			$pageParam += array('parentid'=>0);
		}
		$pageParam += array('from'=>$from);

		$rs = Db::name('area')->where($where)->where('title','like',"%$keyword%")->order($order)
			->paginate([
            	'list_rows'=> $listNum,
            	'query'=> $pageParam,
            ]);
		return $rs;
	}
}